<template>
  <div class="layer" :style="{ '--width': widthVar }">
    <div class="commodity" v-for="(item, index) in listLen" :key="index">
      <!-- @click="toGoodsDetail" 跳转详情的事件-->
      <div class="demo-image" @click="toGoodsDetail(item)">
        <el-image
          style="width: 121px; height: 170px"
          :src="item.pic"
          fit="cover"
        />
        <div class="introduce">
          <span class="demonstration">{{ item.name }}</span>
          <span class="demonstration">{{ item.roughly }}</span>
          <span class="demonstration" style="color: red"
            >￥{{ item.price }}元</span
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { handGoods } from "@/api/api.js";
import { ref, onMounted } from "vue";
import VueEvent from "@/js/mitt.js";
import { useRouter } from "vue-router";
export default {
  name: "GoodsList",
  setup() {
    // 发送请求商品
    onMounted(async () => {
      // 进行axios请求
      await handGoods("get", "goodsList").then((res) => {
        // 解决列表错位
        const len = res.data.length % 3;
        switch (len) {
          case 0:
            widthVar.value = "30%";
            break;
          case 1:
            widthVar.value = "64%";
            break;
          case 2:
            widthVar.value = "30%";
            break;
        }
        listLen.value = res.data;
        console.log(res.data);
      });
    });
    // 商品列表
    const listLen = ref([]);
    const widthVar = ref("");

    VueEvent.on("goodsInfo", (info) => {
      listLen.value = info;
      // 解决列表错位
      const len = info.length % 3;

      switch (len) {
        case 0:
          widthVar.value = "30%";
          break;
        case 1:
          widthVar.value = "64%";
          break;
        case 2:
          widthVar.value = "30%";
          break;
      }
    });
    // 引入路由
    const router = useRouter();
    // 跳转到详情页
    const toGoodsDetail = (e) => {
      router.push({ name: "GoodsDetail", query: e });
    };
    return {
      widthVar,
      listLen,
      // joinShopping,
      toGoodsDetail,
    };
  },
};
</script>

<style lang="scss" scoped>
.layer {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  &:after {
    content: "";
    width: var(--width);
    border: 1px solid transparent;
  }
  .commodity {
    width: 30%;
    height: 170px;
    background-color: #e9ebecde;
    // border: 2px solid #000;
    border-radius: 10px;
    margin-top: 10px;
    margin-bottom: 5px;
    border: 3px solid transparent;
    &:hover {
      border: 3px solid rgb(255, 140, 0);
    }
  }
}
.demo-image {
  display: flex;
  justify-content: flex-start;
  .el-image {
    // margin:4px;
    border-radius: 8px 3px 3px 8px;
    margin-right: 20px;
  }
  .introduce {
    width: 50%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-evenly;
    .demonstration {
      display: block;
      color: var(--el-text-color-secondary);
      font-size: 14px;
      margin-bottom: 20px;
    }
    .g_button {
      width: 50%;
      display: flex;
      justify-content: space-around;
    }
  }
}
</style>